package gui;

import logik.ATM;
import logik.CashDispenser;
import logik.Deposit;
import logik.DepositSlot;
import data.Account;
import data.BankDatabase;
import data.User;
import javax.swing.*;

/**
 * @author Besitzer
 *
 */
public class UserMenu extends JPanel{

	/**
	 * 
	 */
	public boolean userAuthenticated;
	private int currentAccountNumber;
	private BankDatabase bankDatabase;
	private Screen screen;
	private String accountType = null;
	private ATM atm;
	private final int maxAcc = 3;
	private AccountMenu accountmenu;
	private CashDispenser cashDispenser;
	private DepositSlot depositSlot;
	private Deposit envelopeReceived;
	
	/**
	 * @param currentAccountNumber
	 * @param userAuthenticated
	 * @param bankDatabase
	 * @param screen
	 * @param keypad
	 * @param accountType
	 * @param atm
	 * @param maxAcc
	 */
	public UserMenu(int currentAccountNumber, boolean userAuthenticated,
			BankDatabase bankDatabase, Screen screen, 
			String accountType, ATM atm, int maxAcc) {
		this.screen = screen;
		this.currentAccountNumber = 0;
		this.userAuthenticated = false;
		this.bankDatabase = bankDatabase;
		this.accountType = accountType;
		this.atm = atm;
		// this.maxAcc = 3; 
	}

	public int getCurrentAccountNumber(){
		return currentAccountNumber;
	}
	
	/**
	 * 
	 */
	public final void userManagement() {
	
	User currentUser = bankDatabase.getUsers(currentAccountNumber);
	
	// Creating a variable to make the code more readable 
	// and reusing the use of memory
	boolean userExited = false;
	
	while (!userExited) {
		
		while (true) {
	
			int choice = keypad.getInput(3);

			switch (choice) {
			case 2:
				if (currentUser.getAccountList().size() == maxAcc) {
					screen.displayMessageLine("You already have 3 accounts!");
					userManagement();
				}
				screen.displayMessageLine("\nPlease add your preferred account number: ");
				int accountNumber = keypad.getInput(4);
				screen.displayMessageLine("\nAdd an account: ");
				screen.displayMessageLine("\nPlease choose which account type you want to create:  ");
				screen.displayMessageLine("\n1 - Kredit");
				screen.displayMessageLine("2 - Opsparing");
				screen.displayMessageLine("3 - BSU");
				screen.displayMessageLine("\nEnter your choice: ");

				int choose = keypad.getInput(5);
				
				switch(choose){
				case 1:
						accountType = "Kredit";
					break;
				case 2:
						accountType = "Opsparing";
					break;
				case 3:
						accountType = "BSU";
					break;
				default: 
						screen.displayMessageLine("You entered an " 
						+ "invalid choice. Try again!");
				}
				double availableBalance = 0;
				double totalBalance = 0;

				currentUser.addAccount(new Account(accountType, accountNumber, availableBalance, totalBalance));

				if (choose <= maxAcc) {
					screen.displayMessageLine("You have sucessfully " +
							"created an account!");
				}
				break;

			case 3:
				screen.displayMessageLine("\nDelete an account: ");
				for(int i = 0; i < currentUser.getAccountList().size(); i++){
					screen.displayMessageLine("\nPress " + (i+1) + 
							" to delete the selected account" + 
							"\nAccount type: " + currentUser.getSpecificAccount(i).getAccountType() + 
							" | Account number: " + currentUser.getSpecificAccount(i).getAccountNumber());
					screen.displayMessageLine("Avaliable balance: ");screen.displayDollarAmount(currentUser.getSpecificAccount(i).getAvailableBalance());
					screen.displayMessageLine("\nTotal balance: ");screen.displayDollarAmount(currentUser.getSpecificAccount(i).getTotalBalance());
				}
				if (currentUser.getAccountList().size() == 0){ // Checks for accounts to delete
					screen.displayMessageLine("\n No accounts available to delete");
					break;
				}
				screen.displayMessageLine("");
				screen.displayMessageLine("\nEnter your choice: ");

				int remove = keypad.getInput(6);
				if (remove >= 1){ //  A fix to solve a possible crash (-1 index would crash if it's equals 0)
					currentUser.removeAccount(remove - 1);
					screen.displayMessageLine("You have sucessfully deleted an account!\n\n");
				}
				break;

			case 4: 
				screen.displayMessageLine("\nAccount overview: ");
				for(int i = 0; i < currentUser.getAccountList().size(); i++){
					screen.displayMessageLine("\nAccount" + (i+1) + " :"
							+ " Account type: " + currentUser.getSpecificAccount(i).getAccountType() 
							+" | Account number: " + currentUser.getSpecificAccount(i).getAccountNumber());
					screen.displayMessageLine("\nAvaliable balance: ");
					screen.displayDollarAmount(currentUser.getSpecificAccount(i).getAvailableBalance());
					screen.displayMessageLine("\nTotal balance: ");
					screen.displayDollarAmount(currentUser.getSpecificAccount(i).getTotalBalance());
					screen.displayMessageLine("");
				}
				if(currentUser.getAccountList().size() <= 0){
					screen.displayMessageLine("\nNo accounts avaliable. Please add at least one account!\n");
					userManagement();
				}
				break;

			case 1:
				screen.displayMessageLine("\nWhich account do you want to enter? ");
				int accountsListLength = currentUser.getAccountList().size();
				for(int i = 0; i < accountsListLength; i++){
					Account currentAccount = currentUser.getSpecificAccount(i);
					screen.displayMessageLine((i + 1) + " - " + currentAccount.getAccountType());								
				}
				screen.displayMessageLine((accountsListLength + 1) + " - BACK\n");
				screen.displayMessageLine((accountsListLength + 2) + " - EXIT\n ");
				//int 
				int chooseAccount = keypad.getInput(7);

				if(chooseAccount == (accountsListLength + 1)){
					userExited = true; // this ATM session should end
					userManagement();
				}
				else if(chooseAccount == (accountsListLength + 2)){
					screen.displayMessageLine( "\nExiting the system..." );
					atm = new ATM();
					userExited = true;
					userAuthenticated = false;
					atm.run();
				}
				else if (chooseAccount <= accountsListLength && chooseAccount > 0) {
					bankDatabase.getCurrentAccount(chooseAccount);
					accountmenu = new AccountMenu(screen, currentAccountNumber, keypad, 
							cashDispenser, depositSlot, bankDatabase, accountmenu, 
							userExited, accountType, atm, maxAcc, envelopeReceived);
					accountmenu.performTransactions();
				}
				break;

			case 5:
				//atm = new ATM();
				userExited = true;
				userAuthenticated = false; // reset before next ATM session
			//	currentAccountNumber = 0; // reset before next ATM session 
			//	atm.startScreen();
				break;

				} // end switch
			} //end while
		} // end while

	} // end accountManagement()

} // end class
